﻿using System;
using Fly.Data;

namespace FlyWebFrame.Model
{
    public class Categories:SqlServer
    {

        public Categories()
        {
            this.key = "id";
            this.table = "categories";
        }
        public int id;
        public string name;
        public string image;
        public int order;
        public int type;
        public int parentId;
        public string viName;
        public string template;

        public string parentTitle;
        public string parentViTitle;

        public Result Get()
        {
            return this.Select(null, null, "[order]", null);
        }
        public int CountPage(int size)
        {
            int total = (int)this.Count(null).Data;
            int page = total / size;
            if (total % size != 0)
            {
                page++;
            }
            return page;
        }

        public Result GetChild(int parentId)
        {
            this.parentId = parentId;
            return this.Select("parentId = @parentId", null, "[order]", null);
        }

        public Result GetSubCatByType(int type)
        {
            this.type = type;
            return this.Select("parentId <> 0 AND type = @type", null, "[order]", null);
        }

        public Result GetByTemplate(string template)
        {
            this.template = template;
            return this.Select("parentId <> 0 AND template = @template", null, "[order]", null);
        }

        public Result GetPartners()
        {
            return this.Select("parentId <> 0 AND (template = 'partners.htm' OR template = 'shortclients.htm')", null, null, null);
        }

        public Result GetCourses()
        {
            return this.Select("parentId <> 0 AND (template = 'courses.htm' OR template = 'upcommingcourses.htm' OR template = 'pastcourses.htm')", null, null, null);
        }

        public bool Add()
        {
            if(this.image != null)
                return this.Insert("name,image,order,type,parentId,viName").Status;
            return this.Insert("name,order,type,parentId,viName").Status;
        }

        public bool Edit()
        {
            if(this.image != null)
                return this.Update("name,image,order,type,viName").Status;
            return this.Update("name,order,type,viName").Status;
        }
    }
}